package webBeans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import beansInterfaces.BasicProductMgr;
import beansInterfaces.ConstraintsMgr;
import beansInterfaces.PackageMgr;
import dto.ResidenceDTO;

@ManagedBean(name="residenceSearch")
@ViewScoped
public class ResidenceSearchBean implements Serializable{

	private static final long serialVersionUID = 1L;

	@EJB
	private BasicProductMgr basicProductMgr;
	@EJB
	private PackageMgr packageMgr;
	
	@EJB
	private ConstraintsMgr c;
	
	@ManagedProperty(value = "#{packageCreation}")
	private PackageCreationBean pBean;
	@ManagedProperty(value = "#{bookingBean}")
	private BookingBean uBean;
	
	
	
	private List<ResidenceDTO> residences;
	private List<ResidenceDTO> resUser;
	
	private List<ResidenceDTO> filtered;
	private List<ResidenceDTO> filteredUser;
	
	private ResidenceDTO currentResidence;
	private ResidenceDTO modifiedResidence;

		
	public ResidenceSearchBean() {}
	

	@PostConstruct
	public void init()
	{			 
	 	this.filtered = this.basicProductMgr.getAllResidencesDTO();
		this.filteredUser = this.resUser=this.basicProductMgr.getUserResidenceDTO();
	 	
	 	this.residences=this.basicProductMgr.getAllResidencesDTO();
		this.resUser=this.basicProductMgr.getUserResidenceDTO();
		
		
		this.modifiedResidence = new ResidenceDTO();
	}


	//Getters And Setters
	public ResidenceDTO getCurrentResidence() {
			return currentResidence;
		}

	public void setCurrentResidence(ResidenceDTO currentResidence) {
			this.currentResidence= currentResidence;
		}

	public List<ResidenceDTO> getResidences() {
		return residences;
	}

	public void setResidences(List<ResidenceDTO> residences) {
		this.residences = residences;
	}

	public List<ResidenceDTO> getFiltered() {
		return filtered;
	}

	public void setFiltered(List<ResidenceDTO> filtered) {
		this.filtered = filtered;
	}

	public void setpBean(PackageCreationBean pBean) {
		this.pBean = pBean;
	}
	public void setuBean(BookingBean uBean) {
		this.uBean = uBean;
	}
	
	public ConstraintsMgr getcMgr() {
		return c;
	}

	public void setcMgr(ConstraintsMgr cMgr) {
		this.c = cMgr;
	}


	public ResidenceDTO getModifiedResidence() {
		return modifiedResidence;
	}


	public void setModifiedResidence(ResidenceDTO modifiedResidence) {
		this.modifiedResidence = modifiedResidence;
	}
	
	public List<ResidenceDTO> getResUser() {
		return resUser;
	}


	public void setResUser(List<ResidenceDTO> resUser) {
		this.resUser = resUser;
	}

	
	public List<ResidenceDTO> getFilteredUser() {
		return filteredUser;
	}


	public void setFilteredUser(List<ResidenceDTO> filteredUser) {
		this.filteredUser = filteredUser;
	}


	//Employee Methods
	public String modifyResidence() 
	{	
		if(!this.packageMgr.isInPack(this.modifiedResidence)){
			
		this.basicProductMgr.updateResidence(modifiedResidence);
		return "/employee/indexBasicProduct?faces-redirect=true";
		}
		return "error";
	}
	
	
	public String deleteResidence() 
	{	
		if(!this.packageMgr.isInPack(this.currentResidence))
		{
			this.basicProductMgr.deleteResidence(currentResidence);
			this.pBean.resetCurrentPackage();
			return"indexBasicProduct";

		}
		return"error";
	}
	
	public String addToPack()
	{	
		if(this.currentResidence.getDisponibility()>0 && this.currentResidence.isAvailable())
		{
			this.pBean.getCurrentPackage().setResidence(this.currentResidence);
			this.pBean.getCurrentPackage().setPrice(this.c.autoSetPrice(this.pBean.getCurrentPackage()));
			this.currentResidence = new ResidenceDTO();
			return "createPackage";
		}
		return "error";
	}
	
	public void passToModified(){
			
			ResidenceDTO dto = this.currentResidence;
			this.modifiedResidence.setId(dto.getId());
			this.modifiedResidence.setDisponibility(dto.getDisponibility());
			this.modifiedResidence.setAdmin(this.uBean.getUserMgr().getUserDTO().getUsername());
			this.modifiedResidence.setPrice(dto.getPrice());
			this.modifiedResidence.setFromDate(dto.getFromDate());
			this.modifiedResidence.setToDate(dto.getToDate());
			this.modifiedResidence.setAvailable(dto.isAvailable());
			this.modifiedResidence.setResidenceName(dto.getResidenceName());
			this.modifiedResidence.setType(dto.getType());
			this.modifiedResidence.setRooms(dto.getRooms());
			this.modifiedResidence.setBeds(dto.getBeds());
			this.modifiedResidence.setStars(dto.getStars());
			this.modifiedResidence.setCity(dto.getCity());
			this.modifiedResidence.setCountry(dto.getCountry());
			this.modifiedResidence.setAddress(dto.getAddress());
			this.modifiedResidence.setPhoneNumber(dto.getPhoneNumber());
			this.modifiedResidence.setSite(dto.getSite());

	}
	
	
	//User Methods
	
	
	public void changeResidence(){
		if(this.currentResidence.getDisponibility()>0 && this.currentResidence.isAvailable())
		{
			this.uBean.getPackageOnEdit().setResidence(this.currentResidence);
			this.uBean.getPackageOnEdit().setPrice(this.c.autoSetPrice(this.uBean.getPackageSelected()));
			this.currentResidence= new ResidenceDTO();
		}
		
	}




	
	
	
}
